<?php
namespace Mall\Controller;

use Common\Controller\ApiController;
use Common\Model\ModelException;
use Util\ModelUtils;

class GoodsCommentController extends ApiController
{
    public function getByGoods($goodsId)
    {
        $input = $this->request();
        $page = filter($input['page'], 1);
        $perPage = filter($input['per_page'], 10);
        $perPage > 30 && $perPage = 30;

        $goods = D('Goods')->find($goodsId);
        if (empty($goods)) $this->errorResponse(90001);
        $where = [
            'goods_id' => $goodsId,
            'is_checked' => 'Y'
        ];
        $count = D('GoodsComment')->where($where)->count();
        $comments = D('GoodsComment')
            ->where($where)
            ->page($page, $perPage)
            ->order('create_time desc')
            ->select();
        ModelUtils::belongsTo($comments, ['relation' => 'Account/Users', 'foreignKey' => 'user_id', 'refKey' => 'user_id', 'name' => 'user']);
        $comments = transform('GoodsComment', 'withoutGoods', $comments);
        $pagination = pagination($count, $perPage, $page);
        $this->response($comments, $pagination);
    }

    /**
     * 提交商品评价
     *
     * @return void
     */
    public function store($orderId)
    {
        $user = $this->user;
        $Order = D('Order')->find($orderId);
        if (empty($Order)) {
            $this->errorResponse(90001,'订单已经失效');
        }

        $comment = $this->request();
        foreach($comment['reputations'] as $input){
          $input['body'] = trim(filter($input['body'], '')); 
          $input = array_keep($input, ['body','goods_id','pics','reputation']);
          
          if ($input['body'] === '') {
            $this->errorResponse(90007, '评论不可为空');
        }

        if ($input['pics'] && count($picsArr = explode(',', $input['pics'])) > 5) {
            $this->errorResponse(90007, '最多5张图片');
        }
        if($picsArr){
            foreach($picsArr as &$value){
             $imageArr = explode('/', $value,4);
             $value = $imageArr[count($imageArr)-1];
            }
          $input['pics'] = implode(',',$picsArr);
        }
        
        //上传图片
        $commented = D('GoodsComment')->where(['user_id'=>$user['id'],'goods_id'=>$input['goods_id'],'order_id'=>$orderId])->find();
        if($commented){
            $this->errorResponse(90007,'请不要重复发表评论');
        }
        
        $input['user_id'] = $user['id'];
        $input['order_id'] = $orderId;
        $goodsCommentModel = D('GoodsComment');
        if ($goodsCommentModel->create($input)) {
            $goodsCommentModel->add();
            // 评论数加1
            D('Goods')->addComment($input['goods_id']);
            D('OrderGoods')->where(['order_id'=>$orderId,'goods_id'=>$input['goods_id']])->setField('comments_count','1');
            //$comment = D('GoodsComment')->find($id);
            //ModelUtils::belongsTo($comment, ['relation' => 'Account/Users', 'foreignKey' => 'user_id', 'refKey' => 'user_id', 'name' => 'user']);
            //$comment = transform('GoodsComment', 'withoutGoods', $comment);
            $this->createdResponse();
        } else {
            $this->errorResponse(90007, $goodsCommentModel->getError());
        }
        } 
        
        

    }
    
   
}
